<?php
namespace Admin\Controller;
use Admin\Controller;
/**
 * 单页管理
 */
class HouseController extends BaseController
{
    /**
     * 单页列表
     * @return [type] [description]
     */
    public function index($key="")
    {
        if($key === ""){
            $model = M('house');  
        }else{
            // $where['title'] = array('like',"%$key%");
            $where['name'] = array('like',"%$key%");
            // $where['_logic'] = 'or';
            $model = M('house')->where($where); 
        }
        $count  = $model->where($where)->count();// 查询满足要求的总记录数
        $Page = new \Extend\Page($count,15);// 实例化分页类 传入总记录数和每页显示的记录数(25)
        $show = $Page->show();// 分页显示输出
        $pages = $model->join("project on house.project_id=project.id")->join("sales on house.id=sales.house_id")->field("house.id,project.name,house.house_num,house.building,house.unit,house.floor,house.door,house.square,house.room,house.living,house.toilet,house.face,house.size_intro,sales.price,sales.reduce,house.flag")->limit($Page->firstRow.','.$Page->listRows)->where($where)->order('house.id DESC')->select();
        // var_dump($pages);exit;
        $this->assign('model', $pages);
        $this->assign('page',$show);
        $this->display();     
    }

    /**
     * 添加单页
     */
    public function add()
    {
        //默认显示添加表单
        if (!IS_POST) {
            $list = M("Project")->where("flag=1")->select();
            $this->assign("list",$list);
            $this->display();
        }
        if (IS_POST) {
            //如果用户提交数据
            $model = D("House");
			
			

        //上传附件代码2017-3-11-----------------------------------------------------------
		   if(!empty($_FILES)){  //判断是否有文件上传
		   $config = array(
		   'rootPath'      =>  './Public/', //保存根路径
		   'savePath'      =>  'house/', //保存路径
		   );
			//print_r($_FILES); 打印上传图片路径
			$upload = new \Think\Upload($config); //有图片上传实例化uploadOne，把文件上传指定目录
            $cg = $upload -> upload(array($_FILES['pic']));//调用upload函数并将参数$_FILES数组传进去
			if(!$cg){
			show_bug($upload -> getError());//获得上传附件产生的错误信息
			}
			else{
			//循环输出拼装好的图片路径给$bigimg
			foreach($cg as $vo)
			{
			$bigimg.=$vo['savepath'].$vo['savename']."|";
			$_POST['imgurl'] = $bigimg;
			}
			}

            //-------------------------------------------
            $cg1 = $upload -> upload(array($_FILES['pic1']));
			if(!$cg1){
			show_bug($upload -> getError());
			}
			else{
			foreach($cg1 as $vo1)
			{
			$bigimg1.=$vo1['savepath'].$vo1['savename']."|";
			$_POST['imgurl1'] = $bigimg1;
			}
			}

            //-------------------------------------------
            $cg2 = $upload -> upload(array($_FILES['pic2']));
			if(!$cg2){
			show_bug($upload -> getError());
			}
			else{
			foreach($cg2 as $vo2)
			{
			$bigimg2.=$vo2['savepath'].$vo2['savename']."|";
			$_POST['imgurl2'] = $bigimg2;
			}
			}

            //-------------------------------------------
            $cg3 = $upload -> upload(array($_FILES['pic3']));
			if(!$cg3){
			show_bug($upload -> getError());
			}
			else{
			foreach($cg3 as $vo3)
			{
			$bigimg3.=$vo3['savepath'].$vo3['savename']."|";
			$_POST['imgurl3'] = $bigimg3;
			}
			}
			
			}	
			
	
            if (!$model->create()) {
                // 如果创建失败 表示验证没有通过 输出错误提示信息
                $this->error($model->getError());
                exit();
            } else {
                if ($id = $model->add()) {
                    $sales = D("Sales");
                    $sales->create();
                    $sales->house_id = $id;
                    $sales->add();
                    $this->success("添加成功", U('house/index'));
                } else {
                    $this->error("添加失败");
                }
            }
        }
    }
    /**
     * 更新单页信息
     * @param  [type] $id [单页ID]
     * @return [type]     [description]
     */
    public function update($id)
    {
    		$id = intval($id);
        //默认显示添加表单
        if (!IS_POST) {
            $list = M("Project")->where("flag=1")->select();
            $this->assign("list",$list);
            $house = M('house')->field("*,house.introduction as intro")->join("left join project on project.id=house.project_id")->where("house.id=%d",$id)->find();
			
            $house["id"] = $id;
            $sales = M('sales')->where("house_id=%d",$id)->find();
            $this->assign('house',$house);
            $this->assign('sales',$sales);
            $this->display();
        }
        if (IS_POST) {
            if($_GET["type"] == "house"){
                $house = D("house");
				
        //上传附件代码2017-3-11-----------------------------------------------------------
		   if(!empty($_FILES)){  //判断是否有文件上传
		   $config = array(
		   'rootPath'      =>  './Public/', //保存根路径
		   'savePath'      =>  'house/', //保存路径
		   );
			//print_r($_FILES); 打印上传图片路径
			$upload = new \Think\Upload($config); //有图片上传实例化uploadOne，把文件上传指定目录
            $cg = $upload -> upload(array($_FILES['pic']));//调用upload函数并将参数$_FILES数组传进去
			if(!$cg){
			//show_bug($upload -> getError());//获得上传附件产生的错误信息
			}
			else{
			//循环输出拼装好的图片路径给$bigimg
			foreach($cg as $vo)
			{
			$bigimg.=$vo['savepath'].$vo['savename']."|";
			}
			}

            //-------------------------------------------
            $cg1 = $upload -> upload(array($_FILES['pic1']));
			if(!$cg1){
			//show_bug($upload -> getError());
			}
			else{
			foreach($cg1 as $vo1)
			{
			$bigimg1.=$vo1['savepath'].$vo1['savename']."|";
			}
			}

            //-------------------------------------------
            $cg2 = $upload -> upload(array($_FILES['pic2']));
			if(!$cg2){
			//show_bug($upload -> getError());
			}
			else{
			foreach($cg2 as $vo2)
			{
			$bigimg2.=$vo2['savepath'].$vo2['savename']."|";
			}
			}

            //-------------------------------------------
            $cg3 = $upload -> upload(array($_FILES['pic3']));
			if(!$cg3){
			//show_bug($upload -> getError());
			}
			else{
			foreach($cg3 as $vo3)
			{
			$bigimg3.=$vo3['savepath'].$vo3['savename']."|";
			}
			}
			
			}	


				
                if (!$house->create()) {
                    $this->error($house->getError());
                }else{
                    $house->id = $id;
					
                    $data= M('house')->field("imgurl,imgurl1,imgurl2,imgurl3")->where("house.id=%d",$id)->find();
                    //var_dump($data);
			        $house->imgurl=$data['imgurl'].$bigimg;
			        $house->imgurl1=$data['imgurl1'].$bigimg1;
			        $house->imgurl2=$data['imgurl2'].$bigimg2;
			        $house->imgurl3=$data['imgurl3'].$bigimg3;
					
                    if ($house->save()) {
                        $this->success("更新成功", U('house/index'));
                    } else {
                        $this->error("更新失败");
                    }
                }
            }elseif($_GET["type"] == "sales"){
                $sales = D("sales");
                if (!$sales->create()) {
                    $this->error($sales->getError());
                }else{
                    $info = M("sales")->where("house_id=%d",array($id))->find();
                    $sales->id = $info["id"];
                    if ($sales->save()) {
                        $this->success("更新成功", U('house/index'));
                    } else {
                        $this->error("更新失败");
                    }
                }
            }
        }
    }
    /**
     * 删除单页
     * @param  [type] $id [description]
     * @return [type]     [description]
     */
    public function delete($id)
    {
    		$id = intval($id);
        $model = M('house');
        $model->flag = 0; 
        $result = $model->where("id=%d",array($id))->save();
        if($result){
            $this->success("下架成功", U('house/index'));
        }else{
            $this->error("下架失败");
        }
    }
	
         //删除数组指定值	
         public function delarr_1($id){
             header("Content-Type:text/html; charset=utf-8");	
             $tmp = $_POST['url'];
             $data= M('house')->field("imgurl")->where("id=%d",$id)->find();
             $datav=$data['imgurl'];
             $arr = explode("|",$datav);
             foreach( $arr as $k=>$v) {
                if($tmp == $v) unset($arr[$k]);
             }
             $char = implode("|", $arr);
             //echo $char;
             $house = M("house");
             $data['imgurl'] = $char;
             $condition['id'] = $_POST['id'];
             $result = $house ->where($condition)->save($data);
             if($result !== false){
                 echo '数据更新成功！';
             }else{
             echo '数据更新失败！';
             }
             }
	
         //删除数组指定值	
         public function delarr_2($id){
             header("Content-Type:text/html; charset=utf-8");	
             $tmp = $_POST['url'];
             $data= M('house')->field("imgurl1")->where("id=%d",$id)->find();
             $datav=$data['imgurl1'];
             $arr = explode("|",$datav);
             foreach( $arr as $k=>$v) {
                if($tmp == $v) unset($arr[$k]);
             }
             $char = implode("|", $arr);
             //echo $char;
             $house = M("house");
             $data['imgurl1'] = $char;
             $condition['id'] = $_POST['id'];
             $result = $house ->where($condition)->save($data);
             if($result !== false){
                 echo '数据更新成功！';
             }else{
             echo '数据更新失败！';
             }
             }

         //删除数组指定值	
         public function delarr_3($id){
             header("Content-Type:text/html; charset=utf-8");	
             $tmp = $_POST['url'];
             $data= M('house')->field("imgurl2")->where("id=%d",$id)->find();
             $datav=$data['imgurl2'];
             $arr = explode("|",$datav);
             foreach( $arr as $k=>$v) {
                if($tmp == $v) unset($arr[$k]);
             }
             $char = implode("|", $arr);
             //echo $char;
             $house = M("house");
             $data['imgurl2'] = $char;
             $condition['id'] = $_POST['id'];
             $result = $house ->where($condition)->save($data);
             if($result !== false){
                 echo '数据更新成功！';
             }else{
             echo '数据更新失败！';
             }
             }

         //删除数组指定值	
         public function delarr_4($id){
             header("Content-Type:text/html; charset=utf-8");	
             $tmp = $_POST['url'];
             $data= M('house')->field("imgurl3")->where("id=%d",$id)->find();
             $datav=$data['imgurl3'];
             $arr = explode("|",$datav);
             foreach( $arr as $k=>$v) {
                if($tmp == $v) unset($arr[$k]);
             }
             $char = implode("|", $arr);
             //echo $char;
             $house = M("house");
             $data['imgurl3'] = $char;
             $condition['id'] = $_POST['id'];
             $result = $house ->where($condition)->save($data);
             if($result !== false){
                 echo '数据更新成功！';
             }else{
             echo '数据更新失败！';
             }
             }
	


	
}
